import React, { Component } from 'react';
import {
  StyleSheet, View, ScrollView, DeviceEventEmitter,
} from 'react-native';
import produce from 'immer';
import theme from '../../../../native-base-theme/variables';
import CorollaryEquipmentItemImage from './corollary-equipment-item-image';
import * as techDocService from '../../../../services/techDoc';
import { UtilToast } from '../../../../utils/util-method';

class CorollaryEquipment extends Component {
  constructor(props) {
    super(props);
    this.state = {
      datas: [],
    };
  }

  componentDidMount() {
    this.getDatas();
  }

  getDatas = async () => {
    try {
      DeviceEventEmitter.emit('showLoading', true);
      const { datas } = this.props;
      if (datas && datas.equipmentId) {
        const data = await techDocService.getCorollaryEquipment({ mainequipId: datas.equipmentId });
        // const data = await techDocService.getCorollaryEquipment({ mainequipId: '402892e467dea9fd0167e47815362f34' });
        DeviceEventEmitter.emit('showLoading', false);
        if (data && data.length > 0) {
          this.setState(produce(draft => {
            draft.datas = data;
          }));
        } else {
          UtilToast('暂无配套设备信息', 'danger');
        }
      } else {
        DeviceEventEmitter.emit('showLoading', false);
        UtilToast('没有设备信息', 'danger');
      }
    } catch (e) {
      console.log(e);
      DeviceEventEmitter.emit('showLoading', false);
    }
  }

  render() {
    return (
      <View style={styles.container}>
        <ScrollView horizontal showsHorizontalScrollIndicator={false}>
          {this.state.datas.map((data, index) => (<CorollaryEquipmentItemImage item={data} key={index} />))}
        </ScrollView>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: theme.myPrimary,
    flexDirection: 'column',
    padding: 8,
  },
  containerView: {
    flex: 1,
  },
  title: {
    color: 'black',
    fontSize: 17,
    marginTop: 8,
  },
});

export default CorollaryEquipment;
